package com.yanwq.sayhi.controller.club.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yanwq.sayhi.R;
import com.yanwq.sayhi.common.client.MImageLoader;
import com.yanwq.sayhi.controller.club.activity.ClubActivity;
import com.yanwq.sayhi.dao.bean.Club;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：yanweiqiang。
 * 日期：2016/6/16 0016。
 */
public class ClubAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<Club> mList;

    public ClubAdapter() {
        super();
        mList = new ArrayList<>();
    }

    public void refresh(List<Club> clubs) {
        mList.clear();
        mList.addAll(clubs);
        notifyDataSetChanged();
    }

    public void load(List<Club> clubs) {
        mList.addAll(clubs);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_club, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Context context = holder.itemView.getContext();
        ViewHolder viewHolder = (ViewHolder) holder;

        final Club club = mList.get(position);

        MImageLoader.displayImage(club.getPhotoThumbUrl(), viewHolder.avatar);
        viewHolder.name.setText(club.getName());
        viewHolder.motto.setText(club.getMotto());
        viewHolder.permission.setImageResource(getPermissionDrawable(club.getPermission()));

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ClubActivity.class);
                intent.putExtra("club", club);
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    private int getPermissionDrawable(int permission) {
        switch (permission) {
            case 0:
                return R.drawable.ic_person_black_24dp;
            case 1:
                return R.drawable.ic_enhanced_encryption_black_24dp;
            case 2:
                return R.drawable.ic_no_encryption_black_24dp;
            default:
                return 0;
        }
    }

    private class ViewHolder extends RecyclerView.ViewHolder {
        ImageView avatar;
        TextView name;
        TextView motto;
        ImageView permission;

        public ViewHolder(View itemView) {
            super(itemView);
            avatar = (ImageView) itemView.findViewById(R.id.club_avatar);
            name = (TextView) itemView.findViewById(R.id.club_name);
            motto = (TextView) itemView.findViewById(R.id.club_motto);
            permission = (ImageView) itemView.findViewById(R.id.club_permission);
        }
    }
}
